package com.example.shop_2302.userInfo

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2302.R
import com.example.shop_2302.databinding.ActivityEditBinding
import com.example.shop_2302.utils.GlideUtils
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

@Route(path = "/app/EditActivity")
class EditActivity : BaseActivity<ActivityEditBinding,UserViewModel>() {

    var imgPath = ""
    override fun initView() {
        viewModel.user()
        //点击修改密码，跳转到对应界面
        binding.tvPwdV.setOnClickListener {
            ARouter.getInstance().build("/app/UpdateActivity").navigation()
        }

        //点击编辑图标，实现页面上头像变更
        binding.ivEdit.setOnClickListener {
            PictureSelector.create(this,102).selectPicture()
        }
        binding.tvPhone.setOnClickListener {
            //显示生日
            val datePicker = DatePicker(this)
            datePicker.setTitle("选择出生年月日")
            datePicker.wheelLayout.setRange(DateEntity.target(1900,1,1), DateEntity.today(),
                DateEntity.today())//设置显示日期的范围，从19000101-今天  默认显示今天
            datePicker.setOnDatePickedListener { year, month, day ->
                ToastUtils.showLong("$year-$month-$day")
            }
            datePicker.show()
        }
        binding.tvUid.setOnClickListener {
            //显示性别
            val list = mutableListOf("男","女","保密")
            val optionPicker = OptionPicker(this)
            optionPicker.setTitle("选择性别")
            optionPicker.setData(list)
            optionPicker.setOnOptionPickedListener { position, item ->
                ToastUtils.showLong(list[position])
            }
            optionPicker.show()
        }

        //保存用户信息
        binding.btnSave.setOnClickListener {
            //参数是用户头像地址，用户昵称
            //viewModel.save()
        }

    }

    override fun initData() {
        viewModel.userSuccess.observe(this){
            if(it.code==200){
                binding.info = it.data
            }
        }
        viewModel.uploadSuccess.observe(this){
            if(it.code==200){
                imgPath = it.data.url//将成功的用户头像地址保存
            }
        }

    }

    override fun getViewModel(): Class<UserViewModel> = UserViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_edit

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==102&&resultCode==Activity.RESULT_OK){
            //先展示页面上
            val pictureBean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideUtils.loadCircle(this,pictureBean.path,binding.ivImage)
            //调用接口
            val body = RequestBody.create("multipart/form-data".toMediaType(), File(pictureBean.path))
            val part = MultipartBody.Part.createFormData("multipart","aa.png",body)
            viewModel.upload("user",part,"0")
        }
    }

}